#include "mipi_dcs.h"
#include "printf.h"
#include "c800wlac-02-200c-b.h"
#include "systick.h"
#include "i2c.h"

#define LPT_DI 0x39
#define SPT_DI 0x15

uint8_t dcsrst[] = {0x05, 0x01, 0x00};
//*************************************
// Sleep-Out
//*************************************
uint8_t Sleep_Out[] = {0x05, 0x11, 0x00};
uint8_t Sleep_IN[] = {0x05, 0x10, 0x00};
//DELAY	120
// Display-On
//*************************************
uint8_t Display_On[] = {0x05, 0x29, 0x00};

#define CMDNUM (10)

uint8_t dcs0[] = {0x39, 0xB9, 0xFF, 0x83, 0x94};
// Set Power
uint8_t dcs1[] = {0x39, 0xB1, 0x64, 0x10, 0x30, 0x43, 0x34, 0x11, 0xF1, 0x81, 0x70, 0xD9, 0x34, 0x80, 0xC0, 0xD2, 0x41};
// Set Display
uint8_t dcs2[] = {0x39, 0xB2, 0x45, 0x64, 0x0F, 0x09, 0x40, 0x1C, 0x08, 0x08, 0x1C, 0x4D, 0x00, 0x00};
// Set CYC
uint8_t dcs3[] = {0x39, 0xB4, 0x07, 0x6E, 0x07, 0x71, 0x6F, 0x70, 0x00, 0x00, 0x01, 0x6E, 0x0F, 0x6E, 0x07, 0x71, 0x6F, 0x70, 0x00, 0x00, 0x01, 0x6E, 0x0F, 0x6E};
// Set VCOM
uint8_t dcs4[] = {0x39, 0xB6, 0x6F, 0x6F};
// Set PANEL
uint8_t dcs5[] = {0x15, 0xCC, 0x09};
// Set GIP_0
uint8_t dcs6[] = {0x39, 0xD3, 0x00, 0x08, 0x00, 0x01, 0x07, 0x00, 0x08, 0x32, 0x10, 0x0A, 0x00, 0x05, 0x00, 0x20, 0x0A, 0x05, 0x09, 0x00, 0x32, 0x10, 0x08, 0x00, 0x35, 0x33, 0x0D, 0x07, 0x47, 0x0D, 0x07, 0x47, 0x0F, 0x08};
// Set GIP_1
uint8_t dcs7[] = {0x39, 0xD5, 0x03, 0x02, 0x03, 0x02, 0x01, 0x00, 0x01, 0x00, 0x07, 0x06, 0x07, 0x06, 0x05, 0x04, 0x05, 0x04, 0x21, 0x20, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x23, 0x22, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
// Set GAMMA
uint8_t dcs8[] = {0x39, 0xE0, 0x03, 0x17, 0x1C, 0x2D, 0x30, 0x3B, 0x27, 0x40, 0x08, 0x0B, 0x0D, 0x18, 0x0F, 0x12, 0x15, 0x13, 0x14, 0x07, 0x12, 0x14, 0x17, 0x03, 0x17, 0x1C, 0x2D, 0x30, 0x3B, 0x27, 0x40, 0x08, 0x0B, 0x0D, 0x18, 0x0F, 0x12, 0x15, 0x13, 0x14, 0x07, 0x12, 0x14, 0x17};
// Sleep Out
uint8_t dcs9[] = {0x05, 0x11, 0x00};
//delay_ms(250,
// Set BC Control
uint8_t dcs10[] = {0x39, 0xC9, 0x1F, 0x2E, 0x1E, 0x1E, 0x10};
// Display On
uint8_t dcs11[] = {0x05, 0x29, 0x00};

void DcsPktWrite(uint8_t DCS_DI, uint8_t Len, uint8_t *Ptr)
{
	uint8_t i = 0;
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	if (Len == 2) {
		HDMI_WriteI2C_Byte(0x01, 0x0c);
		HDMI_WriteI2C_Byte(0x02, 0x04);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, *Ptr);
		HDMI_WriteI2C_Byte(0x03, *(Ptr + 1));
	} else {
		HDMI_WriteI2C_Byte(0x01, 0x0E);
		HDMI_WriteI2C_Byte(0x02, Len + 6);
		HDMI_WriteI2C_Byte(0x03, DCS_DI);
		HDMI_WriteI2C_Byte(0x03, Len);
		HDMI_WriteI2C_Byte(0x03, 0x00);

		for (i = 0; i < Len; i++) {
			HDMI_WriteI2C_Byte(0x03, *Ptr);
			Ptr++;
		}
	}
	delay_1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}

void Generic_Short_Write_1P(uint8_t data0, uint8_t data1)
{
	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x01, 0x0c);
	HDMI_WriteI2C_Byte(0x02, 0x04);
	HDMI_WriteI2C_Byte(0x03, 0x15);
	HDMI_WriteI2C_Byte(0x03, data0);
	HDMI_WriteI2C_Byte(0x03, data1);
	delay_1ms(1);
	HDMI_WriteI2C_Byte(0x01, 0x00);
}

void LCD_Reset(void)
{
	/*P07 = 0;
	delay_1ms(50);
	P07 = 1;
	delay_1ms(20);*/
	/*HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET);
	delay_1ms(50);
	HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET);
	delay_1ms(250);*/
	//GPIO_BOP(HX8394_RST_GPIO_PORT) = HX8394_RST_PIN;
	//delay_1ms(10);
	GPIO_BC(HX8394_RST_GPIO_PORT) = HX8394_RST_PIN;
	delay_1ms(50);
	GPIO_BOP(HX8394_RST_GPIO_PORT) = HX8394_RST_PIN;
	delay_1ms(500);
}

void InitPanel(void)
{

	HDMI_WriteI2C_Byte(0xff, 0x81);
	HDMI_WriteI2C_Byte(0x0e, 0xef);
	HDMI_WriteI2C_Byte(0x0e, 0xff);
	HDMI_WriteI2C_Byte(0x0b, 0xfe);

	HDMI_WriteI2C_Byte(0xff, 0x86);
	HDMI_WriteI2C_Byte(0x47, 0x01);
	HDMI_WriteI2C_Byte(0x48, 0x01);

	HDMI_WriteI2C_Byte(0xff, 0xd4);
	HDMI_WriteI2C_Byte(0x20, 0x2f);
	HDMI_WriteI2C_Byte(0x21, 0x10);

	//LCD_Reset();
	delay_1ms(200);

	printf("Start initial panel\n");
	DcsPktWrite(dcs9[0], (sizeof(dcs9) - 1), &dcs9[1]);
	delay_1ms(200);
	DcsPktWrite(dcs0[0], (sizeof(dcs0) - 1), &dcs0[1]);
	DcsPktWrite(dcs1[0], (sizeof(dcs1) - 1), &dcs1[1]);
	DcsPktWrite(dcs2[0], (sizeof(dcs2) - 1), &dcs2[1]);
	DcsPktWrite(dcs3[0], (sizeof(dcs3) - 1), &dcs3[1]);
	DcsPktWrite(dcs4[0], (sizeof(dcs4) - 1), &dcs4[1]);
	DcsPktWrite(dcs5[0], (sizeof(dcs5) - 1), &dcs5[1]);
	DcsPktWrite(dcs6[0], (sizeof(dcs6) - 1), &dcs6[1]);
	DcsPktWrite(dcs7[0], (sizeof(dcs7) - 1), &dcs7[1]);
	DcsPktWrite(dcs8[0], (sizeof(dcs8) - 1), &dcs8[1]);
	DcsPktWrite(dcs9[0], (sizeof(dcs9) - 1), &dcs9[1]);
	delay_1ms(200);
	DcsPktWrite(dcs10[0], (sizeof(dcs10) - 1), &dcs10[1]);
	DcsPktWrite(dcs11[0], (sizeof(dcs11) - 1), &dcs11[1]);
	delay_1ms(10);

	printf("\r\nFinish initial panel");
}
